package me.stone.stanimeclient.page.rank

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.tabs.TabLayout
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import me.stone.stanimeclient.MainViewModel
import me.stone.stanimeclient.R
import me.stone.stanimeclient.base.AbsViewBindingFragment
import me.stone.stanimeclient.core.common.extensions.launchAndRepeatWithLifecycle
import me.stone.stanimeclient.core.data.domain.Result
import me.stone.stanimeclient.core.ui.adapters.FanRankAdapter
import me.stone.stanimeclient.databinding.DialogRankYearSelectorBinding
import me.stone.stanimeclient.databinding.FragmentRankBinding
import me.stone.stanimeclient.util.DefaultTabSelectedListener
import javax.inject.Inject

@AndroidEntryPoint
class RankFragment : AbsViewBindingFragment<FragmentRankBinding>() {

    @Inject
    lateinit var adapter: FanRankAdapter

    private val mainViewModel by activityViewModels<MainViewModel>()

    private val rankViewModel by activityViewModels<RankViewModel>()

    private val rankYearSelectorBinding by lazy {
        DialogRankYearSelectorBinding.inflate(layoutInflater).apply {
            viewModel = rankViewModel
            executePendingBindings()
        }
    }

    private val rankYearSelectorDialog by lazy {
        BottomSheetDialog(requireActivity(), R.style.BottomSheetDialog).apply {
            setContentView(rankYearSelectorBinding.root)
        }
    }

    override fun buildViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentRankBinding {
        return FragmentRankBinding.inflate(
            inflater, container, false
        ).apply {
            viewModel = rankViewModel
            executePendingBindings()
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        adapter.setOnItemClickListener {
            mainViewModel.seeFanDetailItem(it.id)
        }

        binding.rankContent.adapter = adapter

        listOf("周榜", "月榜", "总榜").forEach { name ->
            with(binding.rankType) {
                addTab(newTab().apply { text = name })
            }
        }
        binding.rankType.addOnTabSelectedListener(object : DefaultTabSelectedListener() {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                tab?.position?.let { rankViewModel.changeShowPage(it) }
            }
        })

        launchAndRepeatWithLifecycle {
            launch {
                rankViewModel.rankData.collect {
                    if (it is Result.Success) {
                        adapter.submitList(it.data) {
                            binding.rankContent.scrollToPosition(0)
                        }
                    }
                }
            }

            launch {
                rankViewModel.rankYearDisplay.collect {
                    binding.rankYear.text = it
                }
            }

            launch {
                rankViewModel.rankYearDialogSign.collectLatest {
                    if (it) {
                        rankYearSelectorDialog.show()
                    } else {
                        rankYearSelectorDialog.cancel()
                    }
                }
            }
        }
    }

}